/*
 * Created by JFormDesigner on Mon Jun 12 17:15:28 CST 2023
 */

package fm.trade;

import dao.Dao_trade;
import model.Trade;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class LookTrade extends JInternalFrame {

    private String [] columnNames = {"商品编号", "商品名称", "商品类别", "数量", "折扣", "单价"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columnNames.length];

        for(int i = 0; i < list.size(); i++)
        {
            Trade trade = (Trade) list.get(i);

            results[i][0] = trade.getTradeID();
            results[i][1] = trade.getTradeName();
            results[i][2] = Dao_trade.getTradeName_ID( trade.getTypeID());
            results[i][3] = trade.getNumber();
            results[i][4] = trade.getDiscount();
            results[i][5] = trade.getPrice();
        }
        return results;
    }

    public LookTrade() {
        initComponents();
        initData_type();
        iniData();
    }

    public void initData_type(){
        Vector<String> its = Dao_trade.getTradeType();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        LookComboBox.setModel(model);

        String type = (String) LookComboBox.getSelectedItem();

        Object [][] results = getFS(Dao_trade.fuzzySelect(Dao_trade.getTypeID_Name(type)));
        DefaultTableModel model_1 = new DefaultTableModel(results, columnNames);
        table1.setModel(model_1);
    }

    public void iniData()
    {
        Object [][] results = getFS(Dao_trade.fuzzySelectAll());
        DefaultTableModel model = new DefaultTableModel(results, columnNames);
        table2.setModel(model);
    }

    private void Cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void Look(ActionEvent e) {
        // TODO add your code here
        String type = (String) LookComboBox.getSelectedItem();

        Object [][] results = getFS(Dao_trade.fuzzySelect(Dao_trade.getTypeID_Name(type)));
        DefaultTableModel model_1 = new DefaultTableModel(results, columnNames);
        table1.setModel(model_1);
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        tabbedPane1 = new JTabbedPane();
        panel4 = new JPanel();
        scrollPane2 = new JScrollPane();
        panel7 = new JPanel();
        scrollPane3 = new JScrollPane();
        table2 = new JTable();
        panel2 = new JPanel();
        cancelButton = new JButton();
        panel5 = new JPanel();
        LookComboBox = new JComboBox();
        panel6 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel1 = new JPanel();
        CancelButton = new JButton();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u67e5\u770b\u5546\u54c1");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== tabbedPane1 ========
        {

            //======== panel4 ========
            {
                panel4.setLayout(new BorderLayout());

                //======== scrollPane2 ========
                {

                    //======== panel7 ========
                    {
                        panel7.setBorder(new TitledBorder("\u5546\u54c1\u4fe1\u606f"));
                        panel7.setLayout(null);

                        //======== scrollPane3 ========
                        {
                            scrollPane3.setViewportView(table2);
                        }
                        panel7.add(scrollPane3);
                        scrollPane3.setBounds(10, 20, 560, 260);
                    }
                    scrollPane2.setViewportView(panel7);
                }
                panel4.add(scrollPane2, BorderLayout.CENTER);

                //======== panel2 ========
                {
                    panel2.setMinimumSize(new Dimension(302, 40));
                    panel2.setPreferredSize(new Dimension(302, 40));
                    panel2.setLayout(null);

                    //---- cancelButton ----
                    cancelButton.setText("\u8fd4\u56de");
                    cancelButton.addActionListener(e -> cancel(e));
                    panel2.add(cancelButton);
                    cancelButton.setBounds(new Rectangle(new Point(253, 5), cancelButton.getPreferredSize()));

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel2.getComponentCount(); i++) {
                            Rectangle bounds = panel2.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel2.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel2.setMinimumSize(preferredSize);
                        panel2.setPreferredSize(preferredSize);
                    }
                }
                panel4.add(panel2, BorderLayout.SOUTH);
            }
            tabbedPane1.addTab("\u67e5\u770b\u6240\u6709\u5546\u54c1", panel4);

            //======== panel5 ========
            {
                panel5.setLayout(new BorderLayout());

                //---- LookComboBox ----
                LookComboBox.addActionListener(e -> Look(e));
                panel5.add(LookComboBox, BorderLayout.NORTH);

                //======== panel6 ========
                {
                    panel6.setBorder(new TitledBorder("\u5546\u54c1\u4fe1\u606f"));
                    panel6.setLayout(null);

                    //======== scrollPane1 ========
                    {
                        scrollPane1.setViewportView(table1);
                    }
                    panel6.add(scrollPane1);
                    scrollPane1.setBounds(10, 20, 565, 235);

                    //======== panel1 ========
                    {
                        panel1.setLayout(new FlowLayout());

                        //---- CancelButton ----
                        CancelButton.setText("\u8fd4\u56de");
                        CancelButton.addActionListener(e -> Cancel(e));
                        panel1.add(CancelButton);
                    }
                    panel6.add(panel1);
                    panel1.setBounds(0, 260, 583, panel1.getPreferredSize().height);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel6.getComponentCount(); i++) {
                            Rectangle bounds = panel6.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel6.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel6.setMinimumSize(preferredSize);
                        panel6.setPreferredSize(preferredSize);
                    }
                }
                panel5.add(panel6, BorderLayout.CENTER);
            }
            tabbedPane1.addTab("\u6839\u636e\u5546\u54c1\u7c7b\u578b\u67e5\u770b", panel5);
        }
        contentPane.add(tabbedPane1);
        tabbedPane1.setBounds(0, 0, 583, tabbedPane1.getPreferredSize().height);

        contentPane.setPreferredSize(new Dimension(595, 390));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTabbedPane tabbedPane1;
    private JPanel panel4;
    private JScrollPane scrollPane2;
    private JPanel panel7;
    private JScrollPane scrollPane3;
    private JTable table2;
    private JPanel panel2;
    private JButton cancelButton;
    private JPanel panel5;
    private JComboBox LookComboBox;
    private JPanel panel6;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel1;
    private JButton CancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
